/**
 * Created by Yamazhiki on 08/01/2017.
 */
/**
 * Created by Yamazhiki on 08/01/2017.
 */ export var CType;
(function (CType) {
    CType[CType["Char"] = 1] = "Char";
    CType[CType["UChar"] = 1] = "UChar";
    CType[CType["Short"] = 2] = "Short";
    CType[CType["UShort"] = 2] = "UShort";
    CType[CType["Int"] = 4] = "Int";
    CType[CType["UInt"] = 4] = "UInt";
    CType[CType["Int64"] = 8] = "Int64";
    CType[CType["UInt64"] = 8] = "UInt64";
})(CType || (CType = {}));
export class SocketHelper {
    /**
     *
     * @param offest 初始偏移量
     */
    constructor(offest = 0) {
        this.offset = 0;
        this.offset = offest;
    }
    putChar(buffer, value) {
        buffer.writeInt8(value, this.offset);
        this.offset++;
    }
    putUChar(buffer, value) {
        buffer.writeUInt8(value, this.offset);
        this.offset++;
    }
    putShort(buffer, value) {
        buffer.writeUInt16LE(value, this.offset);
        this.offset += 2;
    }
    putUShort(buffer, value) {
        buffer.writeUInt16LE(value, this.offset);
        this.offset += 2;
    }
    putInt(buffer, value) {
        buffer.writeInt32LE(value, this.offset);
        this.offset += 4;
    }
    putUInt(buffer, value) {
        buffer.writeUInt32LE(value, this.offset);
        this.offset += 4;
    }
    putInt64(buffer, value) {
        buffer.writeInt32LE(value, this.offset);
        this.offset += 8;
    }
    putUInt64(buffer, value) {
        buffer.writeInt32LE(value, this.offset);
        this.offset += 8;
    }
    putString(buffer, value, len) {
        if (value) {
            buffer.write(value, this.offset, len);
        }
        else {
            buffer.fill(0, this.offset, this.offset + len);
        }
        this.offset += len;
    }
    getChar(buffer) {
        let result = buffer.readInt8(this.offset);
        this.offset++;
        return result;
    }
    getUChar(buffer) {
        let result = buffer.readUInt8(this.offset);
        this.offset++;
        return result;
    }
    getShort(buffer) {
        let result = buffer.readInt16LE(this.offset);
        this.offset += 2;
        return result;
    }
    getUShort(buffer) {
        let result = buffer.readUInt16LE(this.offset);
        this.offset += 2;
        return result;
    }
    getInt(buffer) {
        let result = buffer.readInt32LE(this.offset);
        this.offset += 4;
        return result;
    }
    getUInt(buffer) {
        let result = buffer.readUInt32LE(this.offset);
        this.offset += 4;
        return result;
    }
    getInt64(buffer) {
        let result = buffer.readUInt32LE(this.offset);
        this.offset += 8;
        return result;
    }
    getUInt64(buffer) {
        let result = buffer.readUInt32LE(this.offset);
        this.offset += 8;
        return result;
    }
    getString(buffer, len, encoding = "ascii") {
        let strBuffer = buffer.slice(this.offset, this.offset + len);
        let useLen = 0;
        for (let i = 0; i < strBuffer.byteLength; i++) {
            if (strBuffer[i] === 0)
                break;
            useLen++;
        }
        this.offset += len;
        return strBuffer.toString(encoding, 0, useLen);
    }
    getCharArray(buffer, len) {
        let tmpBuffer = buffer.slice(this.offset, this.offset + len);
        let rlt = new Array();
        for (let i = 0; i < tmpBuffer.byteLength; i++) {
            rlt.concat(tmpBuffer.readInt8(i));
        }
        this.offset += len;
        return rlt;
    }
    getShortArray(buffer, len) {
        let finalOffset = len * 2;
        let tempBuffer = buffer.slice(this.offset, this.offset + finalOffset);
        let rlt = new Array();
        for (let i = 0; i < tempBuffer.byteLength; i += 2) {
            rlt.splice(i / 2, 0, tempBuffer.readInt16LE(i));
        }
        this.offset += finalOffset;
        return rlt;
    }
    getBuffer(buffer, len) {
        let rlt = buffer.slice(len);
        this.offset += len;
        return rlt;
    }
}
